package com.google.code.swingonfire.component.button;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;

import javax.swing.Action;
import javax.swing.JButton;

import com.google.code.swingonfire.plotter.ArrowSketch;
import com.google.code.swingonfire.plotter.Plotter;
import com.google.code.swingonfire.utility.Colors;

public class JMultiButton extends JButton
{

    private static final long serialVersionUID = 1155566626075242762L;

    private final MultiButtonListModel listModel = new MultiButtonListModel();
    private final Plotter arrowPlotter = new Plotter(new ArrowSketch(0.5, 0, 0.5));

    private Insets originalMargin;
    private int arrowSize = 8;

    public JMultiButton()
    {
        super();
    }

    public void addAction(Action action)
    {
        listModel.addElement(action);

        if (getAction() == null)
        {
            setAction(action);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setMargin(Insets margin)
    {
        Font font = getFont();

        if (margin == null)
        {
            margin = new Insets(0, 0, 0, 0);
        }

        originalMargin = margin;
        arrowSize = (font != null) ? font.getSize() : 8;

        super.setMargin(new Insets(margin.top, margin.left, margin.bottom, margin.right + (arrowSize * 2)));
    }

    @Override
    public void setFont(Font font)
    {
        super.setFont(font);

        setMargin(originalMargin);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        drawArrow((Graphics2D) g.create());
    }

    private void drawArrow(Graphics2D g)
    {
        int width = getWidth();
        int height = getHeight();
        Insets margin = getMargin();

        int topSpacing = 0;
        int bottomSpacing = 0;
        int centerX = 0;

        String uiClassName = getUI().getClass().getName();

        System.out.println(uiClassName);
        
        if (uiClassName.endsWith("MetalButtonUI")) {
            topSpacing = -1;
        }
        else if (uiClassName.endsWith("SynthButtonUI"))
        {
            topSpacing = bottomSpacing = 3;
            centerX = -2;
        }
        else if (uiClassName.endsWith("MotifButtonUI")) {
            topSpacing = 6;
            bottomSpacing = 7;
            centerX = -6;
        }
        else if (uiClassName.endsWith("WindowsButtonUI")) {
            bottomSpacing = 1;
            centerX = -1;
        }

        g.setColor(Colors.translucent(Color.BLACK, 0.25));
        g.drawLine(((width - margin.right) + originalMargin.right), margin.top + topSpacing,
            ((width - margin.right) + originalMargin.right), height - margin.bottom - bottomSpacing);
        g.setColor(Colors.translucent(Color.WHITE, 0.25));
        g.drawLine(((width - margin.right) + originalMargin.right) + 1, margin.top + topSpacing,
            ((width - margin.right) + originalMargin.right) + 1, height - margin.bottom - bottomSpacing);

        g.translate(width - ((margin.right - originalMargin.right - centerX) / 2), height / 2);

        g.setColor(getForeground());
        arrowPlotter.rotate(Math.PI / 2).size(new Dimension((arrowSize * 10) / 8, arrowSize));
        arrowPlotter.draw(g);
    }
}
